package com.vf.admin.miaodong.controller;

import com.vf.admin.client.service.ExchangeGoldDiamondsLogService;
import com.vf.admin.client.service.ExchangeGoldDiamondsService;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.ExchangeGoldDiamonds;
import com.vf.admin.miaodong.entity.ExchangeGoldDiamondsLog;
import com.vf.admin.miaodong.entity.ExpressionsCommon;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 兑换记录管理
 */
@Controller
@RequestMapping("/after/exchange")
public class AfterExchangeController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    ExchangeGoldDiamondsService exchangeGoldDiamondsService;
    @Autowired
    ExchangeGoldDiamondsLogService exchangeGoldDiamondsLogService;

    @RequestMapping("/list")
    public String list(SplitPageBean splitPageBean) {
        try {
            Map<String, Object> map = new HashMap<>();
            List<ExchangeGoldDiamonds> list = exchangeGoldDiamondsService.page(splitPageBean, map);

            request.setAttribute("list", list);
            request.setAttribute("splitPage", splitPageBean);
            return this.sucess("/web/after/exchange/list");
        } catch (Exception e) {
            logger.error("查询兑换管理列表异常", e);
            return this.error("系统繁忙");
        }
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Result saveOrUpdate(ExchangeGoldDiamonds params) {
        try {
            if (StringUtils.isEmpty(params.getImage())) {
                return errorJSON("图片不能为空");
            }
            if (StringUtils.isEmpty(params.getDescript())) {
                return errorJSON("描述不能为空");
            }
            if (params.getGoldCoin().compareTo(BigDecimal.ZERO) == 0) {
                return errorJSON("金额不能为0");
            }
            if (params.getDiamonds().compareTo(BigDecimal.ZERO) == 0) {
                return errorJSON("金额不能为0");
            }
            exchangeGoldDiamondsService.saveOrUpdate(params);
            return sucessJSON("成功");
        } catch (Exception e) {
            return errorJSON("系统繁忙");
        }
    }

    @RequestMapping("/info")
    public String info(Integer id) {
        try {
            ExchangeGoldDiamonds info = exchangeGoldDiamondsService.get(id);
            request.setAttribute("obj", info);
            return this.sucess("/web/after/exchange/edit");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }

    @ResponseBody
    @RequestMapping("/del")
    public Result del(Integer id) {
        exchangeGoldDiamondsService.del(id);
        return sucessJSON("成功");
    }

    @RequestMapping("/gotoAdd")
    public String gotoAdd() {
        return this.sucess("/web/after/exchange/add");
    }

    @RequestMapping("/exchangeLog")
    public String exchangeLog(SplitPageBean splitPageBean) {
        try {
            Map<String, Object> map = new HashMap<>();
            List<ExchangeGoldDiamondsLog> list = exchangeGoldDiamondsLogService.page(map, splitPageBean);

            request.setAttribute("list", list);
            request.setAttribute("splitPage", splitPageBean);
            return this.sucess("/web/after/exchange/exchangelog");
        } catch (Exception e) {
            logger.error("查询兑换管理列表异常", e);
            return this.error("系统繁忙");
        }
    }
}
